//
//  CourseCollectionView.m
//  Civacrm
//
//  Created by 王桂敏 on 2017/2/23.
//  Copyright © 2017年 王桂敏. All rights reserved.
//

#import "CourseCollectionView.h"
#import "CourseCollectionViewCell0.h"

static NSString *CourseCollectionIdentifier = @"CourseCollectionViewCell";


@implementation CourseCollectionView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}
- (void)initView{
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    self.layout.minimumLineSpacing = 0;
    self.layout.minimumInteritemSpacing = 0;
    self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.layout];
    self.collectionView.backgroundColor = WhiteColor;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self addSubview:self.collectionView];
}
- (void)setViewWithType:(CourseCollectionViewType)type
                    col:(NSInteger)col{
    self.type = type;
    self.col = col;
    switch (type) {
        case CourseCollectionViewType0:
        {
            
            [self.collectionView registerClass:[CourseCollectionViewCell0 class] forCellWithReuseIdentifier:CourseCollectionIdentifier];
            
        }
            break;

            
        default:
            break;
    }
    [self.collectionView reloadData];
}


- (void)setDataSource:(NSMutableArray *)dataSource{
    _dataSource = dataSource;
    [self.collectionView reloadData];
}
- (void)setViewWithDataSource:(NSMutableArray *)dataSource col:(NSInteger)col{
    self.col = col;
    [self setDataSource:dataSource];
}
#pragma mark - UICollectionViewDataSource -
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CourseCollectionViewCell0 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CourseCollectionIdentifier forIndexPath:indexPath];
    [cell configCellWithModel:self.dataSource[indexPath.row] col:self.col];
    return cell;

}

#pragma mark 处理点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.selectIndex = indexPath.row;
    [self.collectionView reloadData];
    if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView: didSelect: type:)]) {
        [self.delegate collectionView:self didSelect:self.selectIndex type:self.type];
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout Method
#pragma mark 设置item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (self.type) {
        case CourseCollectionViewType0:
        {
            return CGSizeMake((kScreenWidth - w(60)) / self.col, h(211));
            
        }
            break;
            
        default:
            return CGSizeMake(0, 0);
            break;
    }
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.height = self.height;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
